import { Injectable, OnModuleInit } from '@nestjs/common';
import { RedisService } from '@/shared/redis.service';
import { PermissionService } from '@/modules/permission/permission.service';

// 引入redis服务

@Injectable()
export class PermissionsCacheService implements OnModuleInit {
  constructor(
    private redisService: RedisService,
    private permissionService: PermissionService,
  ) {}

  async onModuleInit() {
    await this.cachePermissions();
  }

  private async cachePermissions(): Promise<void> {
    const permissions = await this.permissionService.findAllPermissions();
    await this.redisService.set('SYSTEM_PERMISSIONS', JSON.stringify(permissions));
    return;
  }
}
